angular
    .module('app.service')
    .service('PaperService', PaperService);

PaperService.$inject = ['API', '$http'];

function PaperService(API, $http) {
    var baseUrl = API + 'papers';
    this.get = get;
    this.getOne = getOne;
    this.list = list;
    this.marking = marking;
    this.markTasks = markTasks;
    this.toggleTag = toggleTag;
    ////////////////

    function get(paperId) {
        return $http.get(baseUrl + '/' + paperId);
    }

    function getOne(params) {
        var urlParams = Object.keys(params).filter((i) => params[i] !== undefined).map((i) => i + '=' + params[i]).join('&');
        return $http.get(baseUrl + '/one?' + urlParams);
    }

    function list(params) {
        var urlParams = Object.keys(params).filter((i) => params[i] !== undefined).map((i) => i + '=' + params[i]).join('&');
        return $http.get(baseUrl + '?' + urlParams);
    }

    function marking(paperId, level, action) {
        var data = {
            "level": level,
            "action": action
        }
        return $http.patch(baseUrl + '/' + paperId, data);
    }

    function markTasks(paperId, stage) {
        var url = baseUrl + '/' + paperId + '/marktasks?stage=' + stage;
        return $http.get(url);
    }

    function toggleTag(paperId) {
        return $http.patch(baseUrl + '/' + paperId + '/tag');
    }
}